package ajax.quanxuan.sys.controller;

import ajax.quanxuan.sys.entity.Leavebill;
import ajax.quanxuan.sys.entity.User;
import ajax.quanxuan.sys.service.LeavebillService;
import ajax.quanxuan.sys.utils.SystemConstant;
import ajax.quanxuan.vo.DataGridViewResult;
import ajax.quanxuan.vo.JSONResult;
import ajax.quanxuan.vo.LeavebillVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/bus/leavebill")
public class LeavbillController {
    @Resource
    private LeavebillService leavebillService;

    @RequestMapping("/leavebillList")
    public DataGridViewResult leavebillList(LeavebillVo leavebillVo, HttpSession session) throws Exception {
        //获取当前登录用户
        User user = (User) session.getAttribute(SystemConstant.LOGINUSER);
        //请假人
        leavebillVo.setUserid(user.getId());
        //创建分页对象
        IPage<Leavebill> page = new Page<Leavebill>(leavebillVo.getPage(), leavebillVo.getLimit());
        //调用分页查询的方法
        leavebillService.findLeaveBillBypage(page, leavebillVo);

        return new DataGridViewResult(page.getTotal(), page.getRecords());
    }

    @RequestMapping("addLeavebill")
    public JSONResult addLeavebill(Leavebill leavebill, HttpSession session) {
        //获取当前登录用户
        User user = (User) session.getAttribute(SystemConstant.LOGINUSER);
        //设置请假人
        leavebill.setUserid(user.getId());
        //设置创建时间
        leavebill.setCreatetime(new Date());
        //设置审批人
        leavebill.setCheckPerson(user.getMgr());
        //状态
        if (leavebill.getState() == SystemConstant.leave_create_state) {
            leavebill.setState(SystemConstant.leave_create_state);
        } else if (leavebill.getState() ==SystemConstant.leave_checking_state) {
            leavebill.setState(SystemConstant.leave_checking_state);
        }

        //调用保存请假的方法
        if (leavebillService.save(leavebill)) {
            return SystemConstant.ADD_SUCCESS;
        }
        return SystemConstant.ADD_ERROR;
    }
    @RequestMapping("updateLeavebill")
    public JSONResult updateLeavebill(Leavebill leavebill) {

        //状态
        if (leavebill.getState() == SystemConstant.leave_create_state) {
            leavebill.setState(SystemConstant.leave_create_state);
        } else if (leavebill.getState() ==SystemConstant.leave_checking_state) {
            leavebill.setState(SystemConstant.leave_checking_state);
            //设置提交时间
            leavebill.setCommittime(new Date());
        }


        //调用保存请假的方法
        if (leavebillService.updateById(leavebill)) {
            return SystemConstant.UPDATE_SUCCESS;
        }
        return SystemConstant.UPDATE_ERROR;
    }
}
